package com.example.ttkl.fragment.XYXActivity;

import static androidx.constraintlayout.helper.widget.MotionEffect.TAG;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.ttkl.Base.BaseActivity;
import com.example.ttkl.Common;
import com.example.ttkl.Constants;
import com.example.ttkl.MyApplication;
import com.example.ttkl.R;
import com.example.ttkl.bean.SaveImageBean;
import com.example.ttkl.fragment.XYXActivity.PT.GameStartActivity;
import com.example.ttkl.interfaces.IBasePresenter;
import com.kc.openset.ad.listener.OSETRewardAdLoadListener;
import com.kc.openset.ad.listener.OSETRewardListener;
import com.kc.openset.ad.reward.OSETRewardAd;
import com.kc.openset.ad.reward.OSETRewardVideo;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class PTXYXActivity extends BaseActivity {
    Button mGameStartButton;
    private Activity mContext;

    @Override
    protected IBasePresenter initPresenter() {
        return null;
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_ptxyx;
    }

    @SuppressLint("CheckResult")
    @Override
    protected void initView() {
        mGameStartButton = findViewById(R.id.activity_main_page_game_start);
        mGameStartButton.setOnClickListener(this::onClick);

        mContext = PTXYXActivity.this;
        OSETRewardVideo.getInstance()
                .setContext(this)
                .setPosId(Common.POS_ID_REWARD_VIDEO)
                .startLoad();
        File file = new File(MyApplication.getPinTu() + "/" + Constants.tupian);
        if (!file.exists()) {
            file.mkdirs();
            Observable.fromCallable(() -> {
                boolean b = saveToSDCard(file);
                return b;
            }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(result -> {

            });
        }
    }

    void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_main_page_game_start:
                OSETRewardVideo.getInstance().loadAd(new OSETRewardAdLoadListener() {
                    @Override
                    public void onLoadSuccess(OSETRewardAd osetRewardAd) {
                        osetRewardAd.showAd(PTXYXActivity.this, osetVideoListener);
                    }

                    @Override
                    public void onLoadFail(String s, String s1) {
                        Toast.makeText(mContext, s+s1, Toast.LENGTH_SHORT).show();
                    }
                });
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    public void showTips(String tips) {

    }

    @Override
    public void showLoading(int visble) {

    }

    //添加示例伴奏到本地
    public boolean saveToSDCard(File file) {
        ArrayList<SaveImageBean> mList = new ArrayList<>();
        mList.add(new SaveImageBean("pt1.png", R.raw.pt1));
        mList.add(new SaveImageBean("pt2.png", R.raw.pt2));
        mList.add(new SaveImageBean("pt3.png", R.raw.pt3));
        mList.add(new SaveImageBean("pt4.png", R.raw.pt4));
        mList.add(new SaveImageBean("pt5.png", R.raw.pt5));

        for (int i = 0; i < mList.size(); i++) {
            try {
                saveImage(file, mList.get(i).getName(), mList.get(i).getRaw());
            } catch (Throwable e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }

    public void saveImage(File file, String name, int raw) throws Throwable {
        InputStream inStream = getResources().openRawResource(raw);
        File file1 = new File(file.getPath() + "/", name);
        FileOutputStream fileOutputStream = null;//存入SDCard
        try {
            fileOutputStream = new FileOutputStream(file1);
            byte[] buffer = new byte[10];
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            int len = 0;
            while ((len = inStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, len);
            }
            byte[] bs = outStream.toByteArray();
            fileOutputStream.write(bs);
            outStream.close();
            inStream.close();
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    private final OSETRewardListener osetVideoListener = new OSETRewardListener() {
        @Override
        public void onShow() {
            Log.d(TAG, "onShow");
        }

        @Override
        public void onError(String s, String s1) {
            Log.d(TAG, "onError");
            Intent intent1 = new Intent(mContext, GameStartActivity.class);
            startActivity(intent1);
        }

        @Override
        public void onClick() {
            Log.d(TAG, "onClick");
        }

        @Override
        public void onClose() {
            Log.d(TAG, "onClose");
            Intent intent1 = new Intent(mContext, GameStartActivity.class);
            startActivity(intent1);
        }

        @Override
        public void onVideoEnd() {
            Log.d(TAG, "onVideoEnd");
        }

        @Override
        public void onVideoStart() {
            Log.d(TAG, "onVideoStart");
        }

        @Override
        public void onReward() {
            Log.d(TAG, "onReward");
        }

        @Override
        public void onServiceResponse(int i) {
        }
    };

}
